import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:series_play/widget/app_color.dart';
import 'package:series_play/widget/app_text.dart';

import '../../utils/log.dart';

class AppSimpleDialog extends StatelessWidget {
  const AppSimpleDialog({
    this.title,
    this.content,
    this.cancelText,
    this.confirmText,
    this.onConfirm,
    this.onCancel,
    this.child,
    this.suffixChild,
    super.key,
  });

  final String? title;
  final String? content;
  final String? cancelText;
  final String? confirmText;
  final VoidCallback? onConfirm;
  final VoidCallback? onCancel;
  final Widget? child;
  final Widget? suffixChild;

  @override
  Widget build(BuildContext context) {
    bool showTitle = title != null && title!.isNotEmpty;
    bool showContent = content != null && content!.isNotEmpty;

    return Material(
      color: Colors.transparent,
      child: Column(
        children: [
          Row(
            children: [
              const Spacer(),
              GestureDetector(
                onTap: () {
                  Navigator.of(context).pop();
                },
                child: Icon(Icons.close, color: AppColor.white_60, size: 18.w),
              ),
            ],
          ),
          10.verticalSpace,

          showTitle
              ? Padding(
                  padding: const EdgeInsets.only(bottom: 6).w,
                  child: AppText.title(title ?? '', color: AppColor.white),
                )
              : Container(),

          showContent
              ? Padding(
                  padding: const EdgeInsets.only(bottom: 6).w,
                  child: AppText.secondary(
                    content ?? '',
                    color: AppColor.white,
                  ),
                )
              : Container(),

          child != null
              ? Padding(
                  padding: const EdgeInsets.only(bottom: 6).w,
                  child: child,
                )
              : Container(),

          30.verticalSpace,
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 32).w,
            child: Row(
              children: [
                cancelText == null || cancelText!.isEmpty
                    ? Container()
                    : _buildButton(
                        AppText.secondary(cancelText!, color: AppColor.white),
                        () {
                          Navigator.of(context).pop();
                          onCancel?.call();
                        },
                      ),

                cancelText != null &&
                        cancelText!.isNotEmpty &&
                        confirmText != null &&
                        confirmText!.isNotEmpty
                    ? 16.horizontalSpace
                    : 0.horizontalSpace,

                confirmText == null || confirmText!.isEmpty
                    ? Container()
                    : _buildButton(
                        AppText.secondary(confirmText!, color: AppColor.white),
                        () {
                          Navigator.of(context).pop();
                          onConfirm?.call();
                        },
                      ),
              ],
            ),
          ),
          suffixChild ?? Container(),
        ],
      ),
    );
  }

  Widget _buildButton(Widget child, VoidCallback? onTap) {
    return Expanded(
      child: GestureDetector(
        onTap: onTap,
        behavior: HitTestBehavior.translucent,
        child: Container(
          height: 36.w,
          decoration: BoxDecoration(
            color: AppColor.white_40,
            borderRadius: BorderRadius.circular(6).r,
          ),
          alignment: Alignment.center,
          child: child,
        ),
      ),
    );
  }
}
